﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class MaintenanceOrderService : BaseService, IMaintenanceOrderService
    {
        private readonly IMaintenanceOrderRepository _maintenanceOrder;
        private readonly IUnitOfWork unitOfWork;
        public MaintenanceOrderService(IMapper mapper, IMaintenanceOrderRepository maintenanceOrder, IUnitOfWork unitOfWork) : base(mapper)
        {
            _maintenanceOrder = maintenanceOrder;
            this.unitOfWork = unitOfWork;
        }

        public bool AddOrModefiyMaintenanceOrder(MaintenanceOrder model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _maintenanceOrder.UpdEntity(model);
            }
            else
            {
                _maintenanceOrder.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelMaintenanceOrder(long maintenanceOrderId)
        {
            var Quote = _maintenanceOrder.QueryEntityById(maintenanceOrderId);
            if (Quote == null)
            {
                return false;
            }
            _maintenanceOrder.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public MaintenanceOrder? GetMaintenanceOrder(long maintenanceOrderId)
        {
            return _maintenanceOrder.QueryEntityById(maintenanceOrderId);
        }

        public List<MaintenanceOrder> GetMaintenanceOrderData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _maintenanceOrder.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d =>d.TechnicianName.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
    }
}
